package problems.daily;

import utils.beans.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * 791. 自定义字符串排序
 * <p>https://leetcode.cn/problems/custom-sort-string/</p>
 *
 * @author habitplus
 * @since 15:56, 2022/11/13
 */
public class T791 {
    public String customSortString(String order, String s) {

        int[] cntMap = new int[26];

        for (char c : s.toCharArray()) {
            ++cntMap[c - 'a'];
        }

        StringBuilder ret = new StringBuilder();
        int k;

        for (char ch : order.toCharArray()) {
            k = cntMap[ch - 'a'];
            cntMap[ch - 'a'] = 0;
            while (k > 0) {
                ret.append(ch);
                --k;
            }

        }

        for (int i = 0; i < 26; ++i) {
            k = cntMap[i];
//            char c = (char) ('a' + i);
            while (k > 0) {
                ret.append((char) ('a' + i));
                --k;
            }
        }

        return ret.toString();
    }
}
